package com.greendeliver.activities;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.test.R;
import com.greendeliver.business.EventManager;

public class EventScheduleDetailActivity extends ListActivity {

	private static final String					TAG_SUCCESS	= "success";
	private static final String					TAG_ID		= "ID_event";
	private static final String					TAG_BEGIN	= "begin_hour_schedule";
	private static final String					TAG_END		= "end_hour_schedule";

	private ArrayList<HashMap<String, String>>	schedulesList;
	private static String						ID_EVENT;

	private ProgressDialog						pDialog;
	private Intent								i;

	@Override
	protected void onCreate( Bundle savedInstanceState ) {
		super.onCreate( savedInstanceState );
		setContentView( R.layout.activity_event_schedule_detail );

		this.schedulesList = new ArrayList<HashMap<String, String>>();
		this.pDialog = new ProgressDialog( EventScheduleDetailActivity.this );

		Intent i = getIntent();
		this.ID_EVENT = i.getStringExtra( TAG_ID );

		Log.d( ID_EVENT, "ID CONNARD" );

		new Load().execute();

		ListView lv = getListView();

	}

	class Load extends AsyncTask<String, String, String> {

		/**
		 * Before starting background thread Show Progress Dialog
		 * */
		@Override
		protected void onPreExecute()
		{
			Log.d( "Coucou", "1" );
			super.onPreExecute();
			pDialog.setMessage( "Chargement des Horaires" );
			pDialog.setIndeterminate( false );
			pDialog.setCancelable( false );
			pDialog.show();
			Log.d( "Coucou", "2" );
		}

		/**
		 * getting All products from url
		 * */
		@Override
		protected String doInBackground( String... args )
		{
			Log.d( "Coucou", "3" );
			EventManager em = new EventManager();
			Log.d( "Coucou", "4" );
			schedulesList = em.get_sched_by_ID( ID_EVENT );
			Log.d( "Coucou", "5" );

			return null;
		}

		/**
		 * After completing background task Dismiss the progress dialog
		 * **/
		@Override
		protected void onPostExecute( String file_url )
		{
			Log.d( "Coucou", "6" );
			pDialog.dismiss();
			Log.d( "Coucou", "7" );
			ListAdapter adapter = new SimpleAdapter( EventScheduleDetailActivity.this,
					schedulesList, R.layout.list_item_schedule,
					new String[] { TAG_BEGIN, TAG_END },
					new int[] { R.id.begin_hour_event, R.id.end_hour_event } );
			setListAdapter( adapter );
			Log.d( "Coucou", "8" );
		}
	}

}
